﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web;
using DHQGHN.DAL;
using DHQGHN.DAL.BO.DanhMuc;
using DHQGHN.DAL.BO.TDT;
using DHQGHN.DAL.Models.DanhMuc;
using DHQGHN.DAL.Models.Online;
using DHQGHN.DAL.Models.TDT;
using DHQGHN.DAL.ModelsUpload;

namespace DHQGHN.Services.TDT
{
    public class HouseHoldService : IHouseHoldService
    {
        private HouseHoldBO householdBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HouseHoldService));

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        public HouseHoldService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;           
            householdBO = new HouseHoldBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion

        #region Service Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eaCode"></param>
        /// <returns></returns>
        public IEnumerable<HOUSEHOLD> GetAllByEACode(string eaCode)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<HOUSEHOLD>();
                }
                else
                {
                    if (string.IsNullOrEmpty(eaCode))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    return householdBO.GetAllByEACode(eaCode);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eaCode"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIdx"></param>
        /// <returns></returns>
        public IEnumerable<HOUSEHOLD> GetAllByEACodePaging(string eaCode,
            string pageSize, string pageIdx)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<HOUSEHOLD>();
                }
                else
                {
                    if (string.IsNullOrEmpty(eaCode)
                        || string.IsNullOrEmpty(pageSize)
                        || string.IsNullOrEmpty(pageIdx))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    int ipageSize, ipageIdx;
                    if (int.TryParse(pageSize, out ipageSize) == false
                        || int.TryParse(pageIdx, out ipageIdx) == false)
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Tham số PageSize (hoặc PageIdx) sai định dạng!",
                            Description = "Tham số PageSize (hoặc PageIdx) sai định dạng!"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    return householdBO.GetAllByEACodePaging(eaCode, ipageSize, ipageIdx).Data;
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="household"></param>
        public void AddHouseHold(HOUSEHOLD household)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.Save(household);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="householdID"></param>
        public void DeleteHouseHold(string householdID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.Delete(householdID);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="household"></param>
        public void EditHouseHold(HOUSEHOLD household)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.Save(household);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public HOUSEHOLD2 GetByID(string houseHoldID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new HOUSEHOLD2();
                }
                else
                {
                    string id = "-1";

                    if (string.IsNullOrEmpty(houseHoldID))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }

                    id = houseHoldID;
                    return householdBO.GetById(id);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// phunx1, 20150520: method luu thong tin vao hai bang HouseHold va Address
        /// </summary>
        /// <param name="bangKeModel"></param>
        public void AddHouseHoldAddress(BangKe bangKeModel)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.SaveHouseHoldAddress(bangKeModel, "C");
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// phunx1, 20150520: method get du lieu tu household va address theo householdid
        /// </summary>
        /// <param name="houseHoldID"></param>
        /// <returns></returns>
        public BangKe2 GetHouseHoldAddressByID(string houseHoldID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new BangKe2();
                }
                else
                {
                    string id = "-1";

                    if (string.IsNullOrEmpty(houseHoldID))
                    {
                        Fault.ValidationFault fault = new Fault.ValidationFault()
                        {
                            Result = false,
                            Message = "Thiếu tham số",
                            Description = "Thiếu tham số"
                        };

                        throw new FaultException<Fault.ValidationFault>(fault);
                    }
                    id = houseHoldID;

                    return householdBO.GetHouseHoldAddressByID(id);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public void EditHouseHoldAddress(BangKe bangKeModel)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.SaveHouseHoldAddress(bangKeModel, "E");
                }
            }
            catch (Exception ex)
            {
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        #endregion

        public List<HOUSEHOLD2> SearchHouseHold(SearchHouseHold search)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<HOUSEHOLD2>();
                }
                else
                {
                    return householdBO.SearchHouseHold(search);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw ex;
            }
        }

        public List<HOUSEHOLD2> GetByAreaCode(string AreaCode)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<HOUSEHOLD2>();
                }
                else
                {
                    List<HOUSEHOLD2> ret = householdBO.GetByAreaCode(AreaCode);
                    return ret;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        /// <summary>
        /// datlq, 20150616: MappingPTDT
        /// </summary>
        /// <param name="map"></param>
        public void MappingPTDT(MappingPTDT map)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.MappingPTDT(map);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// datlq, 20150616: MappingPTDT
        /// </summary>
        /// <param name="map"></param>
        public List<MappingPTDT> SetPTDT_Count(MappingPTDT map)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<MappingPTDT>();
                }
                else
                {
                    return householdBO.SetPTDT_Count(map);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw ex;
            }
        }


        public List<PM3_HOUSEHOLD_PRINTCOUNT> GetByAreaID(string AreaID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<PM3_HOUSEHOLD_PRINTCOUNT>();
                }
                else
                {
                    return householdBO.GetByAreaID(AreaID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }


        public List<PM3_HOUSEHOLD_PRINTCOUNT> GetHouseHoldByAreaIDHouseHoldID(string AreaID, string HouseHoldID)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<PM3_HOUSEHOLD_PRINTCOUNT>();
                }
                else
                {
                    return householdBO.GetHouseHoldByAreaIDHouseHoldID(AreaID, HouseHoldID);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        /// <summary>
        /// datlq, 20150625: ChangingPTDT
        /// </summary>
        /// <param name="map"></param>
        public void ChangingPTDT(MappingPTDT map)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.ChangingPTDT(map);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<MappingPTDT> ChangePTDT_Count(MappingPTDT map)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<MappingPTDT>();
                }
                else
                {
                    return householdBO.ChangePTDT_Count(map);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw ex;
            }
        }

        public List<HouseDetail> GetAllByUserName(string UserName)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<HouseDetail>();
                }
                else
                {
                    return householdBO.GetAllByUserName(UserName);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw ex;
            }
        }

        public void EditHDC(EditHoDanCu ea)
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                }
                else
                {
                    householdBO.EditHDC(ea);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<PARAMETER> GetEndDate()
        {
            try
            {
                if (!RoleChecking.CanAccess(WebOperationContext.Current.IncomingRequest.Headers))
                {
                    RoleChecking.ThrowUnauthorizedException();
                    return new List<PARAMETER>();
                }
                else
                {
                    List<PARAMETER> ret = householdBO.GetEndDate();
                    return ret;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
    }
}